package com.mass.core.sys.sysLog.services.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import com.mass.core.sys.sysLog.dao.SysLogMapper;
import com.mass.core.sys.sysLog.model.SysLog;
import com.mass.core.sys.sysLog.model.SysLogExample;
import com.mass.core.sys.sysLog.services.SysLogServices;
import com.mass.core.utils.AjaxResponse;
import com.mass.core.utils.DateUtils;

@Service
public class SysLogServicesImpl implements SysLogServices {

	@Autowired
	private SysLogMapper logMapper;
	
	
	public int insert(String requestUri, String moduleName, String requestIp,
			Long sysUserId, String sysUserAccount, String requestMethod ,String requestParam,
			String operationDesc, String menuName) {
		
		SysLog sysLog = new SysLog(requestUri, moduleName, requestIp, sysUserId, sysUserAccount, requestMethod, requestParam, operationDesc, menuName);
		sysLog.setCreateTime(DateUtils.getDate());
		sysLog.setDelFlag(0);
		return logMapper.insertSelective(sysLog);
	}

	public AjaxResponse findSysLogPage(Integer page,Integer limit,SysLog sysLog) {
		SysLogExample logExample = new SysLogExample();
		if(!ObjectUtils.isEmpty(page)){
			logExample.setPageIndex((page-1)*limit);
			logExample.setPageSize(limit);
		}
		logExample.setOrderByClause(" CREATE_TIME DESC");
		SysLogExample.Criteria criteria = logExample.createCriteria();
		if(!ObjectUtils.isEmpty(sysLog)){
			if(!ObjectUtils.isEmpty(sysLog.getBeginTime()) && !ObjectUtils.isEmpty(sysLog.getEndTime())){
				criteria.andCreateTimeBetween(sysLog.getBeginTime(), sysLog.getEndTime());
			}
			if(!ObjectUtils.isEmpty(sysLog.getSysUserAccount())){
				criteria.andSysUserAccountLike("%"+sysLog.getSysUserAccount()+"%");
			}
			if(!ObjectUtils.isEmpty(sysLog.getRequestMethod())){
				criteria.andRequestMethodLike("%"+sysLog.getRequestMethod()+"%");
			}
			if(!ObjectUtils.isEmpty(sysLog.getOperationDesc())){
				criteria.andOperationDescLike("%"+sysLog.getOperationDesc()+"%");
			}
			if(!ObjectUtils.isEmpty(sysLog.getMenuName())){
				criteria.andMenuNameLike("%"+sysLog.getMenuName()+"%");
			}
		}
		
		List<SysLog> sysLogList = logMapper.selectByExample(logExample);
		long count = logMapper.countByExample(logExample);
		return AjaxResponse.success("ok", sysLogList, count, page);
	}

	public AjaxResponse delete(Long id) {
		return AjaxResponse.success(logMapper.deleteByPrimaryKey(id));
	}

}
